// /////////////////////////////////////////////////////////////////////////////
//
// Name:            SettingsFrame.cpp
// Author:          Michael Bartsch
//
// Desc :           This file contains the CSettingsFrame Class. This class
//                  contains the Settings Frame which can be used to change
//                  various settings of Aia|CP.
//
// License:         Copyright (C) 2008-2009 Michael Bartsch
//
//                  This program is free software: you can redistribute it
//                  and/or modify it under the terms of the GNU General Public
//                  License as published by the Free Software Foundation,
//                  either version 3 of the License, or (at your option) any
//                  later version.
//
//                  This program is distributed in the hope that it will be
//                  useful, but WITHOUT ANY WARRANTY; without even the implied
//                  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//                  PURPOSE. See the GNU General Public License for more
//                  details.
//
//                  You should have received a copy of the GNU General Public
//                  License along with this program. If not, see
//                  <http://www.gnu.org/licenses/>.
//
// /////////////////////////////////////////////////////////////////////////////

// Include required header file(s).
#include "SettingsFrame.h"


// CSettingsFrame Events. - [ BEGIN ]
BEGIN_EVENT_TABLE(CSettingsFrame, wxFrame)
   EVT_CLOSE(CSettingsFrame::onClose)

   EVT_BUTTON(wxID_CANCEL,   CSettingsFrame::onCancel)
   EVT_BUTTON(wxID_OK,       CSettingsFrame::onOk)
END_EVENT_TABLE()
// CSettingsFrame Events. - [ END ]


// CSettingsFrame.
// Constructor.
CSettingsFrame::CSettingsFrame(wxWindow *parent)
: wxFrame(parent, wxID_ANY, _T("Aia|CP - Settings"), wxPoint(0,0), wxSize(600,480), wxDEFAULT_FRAME_STYLE & ~ (wxRESIZE_BORDER | wxRESIZE_BOX | wxMAXIMIZE_BOX))
{
    // Set internal variables.
    frameParent = (CMainFrame*)parent;

    // Main sizer.
    wxPanel *mainPanel = new wxPanel(this, -1, wxDefaultPosition, wxSize(600, 480));
    wxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);

    // Settings grid.
    settingsGrid = new wxGrid(mainPanel, wxID_ANY, wxDefaultPosition, wxSize(590, 440), wxWANTS_CHARS | wxBORDER_SUNKEN);
    settingsGrid->CreateGrid(2, 2);

    settingsGrid->EnableDragGridSize(false);

    settingsGrid->SetColLabelSize(0);
    settingsGrid->SetColSize(0, 290);
    settingsGrid->SetColSize(1, 290);
    settingsGrid->SetRowLabelSize(0);

    settingsGrid->SetReadOnly(0, 0, true);
    settingsGrid->SetCellValue(0, 0, _T("Aia Port"));
    settingsGrid->SetReadOnly(1, 0, true);
    settingsGrid->SetCellValue(1, 0, _T("Camera Port"));

    wxString tempString;

    tempString.clear();
    tempString.Printf(wxT("%d"), (int)CCommunicator::Instance()->getSelectedDevice());
    settingsGrid->SetCellValue(0, 1, tempString);

    tempString.clear();
    tempString.Printf(wxT("%d"), (int)CColourAnalyser::Instance()->getSelectedDevice());
    settingsGrid->SetCellValue(1, 1, tempString);


    mainSizer->Add(settingsGrid, wxSizerFlags().Expand().Border(wxTOP | wxBOTTOM, 2));

    // Set the main sizer.
    wxSizer *tempSizer = new wxBoxSizer(wxHORIZONTAL);
    tempSizer->Add(new wxButton(mainPanel, wxID_CANCEL, _T("Cancel"), wxDefaultPosition, wxSize(100,30)), wxSizerFlags().Border(wxRIGHT, 10));
    tempSizer->Add(new wxButton(mainPanel, wxID_OK, _T("Ok"), wxDefaultPosition, wxSize(100,30)));
    mainSizer->Add(tempSizer, wxSizerFlags().Centre().Border(wxTOP, 3));

    mainPanel->SetSizer(mainSizer);
    mainSizer->SetSizeHints(mainPanel);
}

// Event Functions. - [ BEGIN ]
void CSettingsFrame::onClose(wxCloseEvent &event)
{
    // Reset frame state.
    MakeModal(false);

    // Destroy frame.
    Destroy();
}

void CSettingsFrame::onCancel(wxCommandEvent& event)
{
    Close();
}

void CSettingsFrame::onOk(wxCommandEvent& event)
{
    // Get values from the settings grid.
    int aiaID = atoi(settingsGrid->GetCellValue(0, 1));
    CCommunicator::Instance()->serialConnect(aiaID);

    int camID = atoi(settingsGrid->GetCellValue(1, 1));
    CColourAnalyser::Instance()->connect(camID);

    // Close the frame.
    Close();
}
// Event Functions. - [ END ]

// End of File
